package com.babyqiao.wallpaper.portal.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.babyqiao.wallpaper.portal.entity.WallpaperAlbum;
import com.babyqiao.wallpaper.portal.entity.WallpaperAlbumPic;
import com.babyqiao.wallpaper.portal.dao.WallpaperAlbumPicMapper;
import com.babyqiao.wallpaper.portal.entity.vo.AlbumPicStatisVO;
import com.babyqiao.wallpaper.portal.entity.vo.AlbumStatisVO;
import com.babyqiao.wallpaper.portal.service.IWallpaperAlbumPicService;
import com.babyqiao.wallpaper.portal.utils.common.RedisKeys;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * <p>
 * 专辑图片信息 服务实现类
 * </p>
 *
 * @author QLZ
 * @since 2021-02-24
 */
@Service
public class WallpaperAlbumPicServiceImpl extends ServiceImpl<WallpaperAlbumPicMapper, WallpaperAlbumPic> implements IWallpaperAlbumPicService {
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    WallpaperAlbumPicMapper albumPicMapper;

    /**
     * 增加图片热度次数
     *
     * @param picId : 图片ID
     * @Author QLZ
     * @Date 2021/2/26
     * @return: AlbumPicStatisVO 最新热度次数
     */
    @Override
    public AlbumPicStatisVO updateStatusService(String picId) {
        Boolean hasKey = redisTemplate.hasKey(RedisKeys.ALBUM_PIC_STATIS.getKey() + picId);
        AlbumPicStatisVO vo=null;
        //如果没有key则说明未缓存过数据
        if(!hasKey){
            WallpaperAlbumPic wallpaperAlbumPic = albumPicMapper.selectById(picId);
            vo=new AlbumPicStatisVO();
            vo.setId(wallpaperAlbumPic.getId());
            vo.setHot(Long.parseLong(wallpaperAlbumPic.getHot()));
            vo.setShare(Long.parseLong(wallpaperAlbumPic.getShare()));

        }else{
            String s = redisTemplate.opsForValue().get(RedisKeys.ALBUM_PIC_STATIS.getKey() + picId);
            vo = JSONObject.parseObject(s, AlbumPicStatisVO.class);
        }
        vo.setHot(vo.getHot()+1);
        redisTemplate.opsForValue().set(RedisKeys.ALBUM_PIC_STATIS.getKey()+picId,JSONObject.toJSONString(vo));

        return vo;
    }

    /***
     *点赞/取消点赞
     * @Author QLZ
     * @Date 2021/2/26
     *  @param picId : 图片ID
     * @param userId: 用户ID
     *  @param value: 1->点赞，其他->取消点赞
     *  @return: boolean
     */
    @Override
    public boolean likeItService(String picId,String userId,String value) {
        boolean var= "1".equals(value);
        return redisTemplate.opsForValue().setBit(RedisKeys.ALBUM_PIC_LIKE.getKey() + picId, Long.parseLong(userId), var);
    }

    /***
     *获得用户对图片的点赞状态
     * @Author QLZ
     * @Date 2021/2/26
     * @param picId : 图片ID
     * @param userId : 用户ID
     @return: java.lang.Integer
     */
    @Override
    public Boolean getLikeItStatusService(String picId, String userId) {

        Boolean bit = redisTemplate.opsForValue().getBit(RedisKeys.ALBUM_PIC_LIKE.getKey() + picId, Long.parseLong(userId));
        return bit;
    }

    /**
     * 获得点赞总数
     *
     * @param picId : 图片ID
     * @Author QLZ
     * @Date 2021/2/28
     * @return: null
     */
    @Override
    public Long getLikeCountService(String picId) {
        Boolean hasKey = redisTemplate.hasKey(RedisKeys.ALBUM_PIC_LIKE.getKey() + picId);
        Long counts=null;
        if(hasKey){
            counts = redisTemplate.execute((RedisCallback<Long>) con -> {
                byte[] bytes = (RedisKeys.ALBUM_PIC_LIKE.getKey() + picId).getBytes();
                Long aLong = con.bitCount(bytes);
                return aLong;
            });
        }else {
            WallpaperAlbumPic wallpaperAlbumPic = baseMapper.selectById(picId);
            counts = Long.parseLong(wallpaperAlbumPic.getLikeIt()) ;

        }

        return  counts;
    }

    /***
     增加图片分享次数
     * @Author QLZ
     * @Date 2021/2/26
     *  @param picId : 图片ID
     *  @return: com.babyqiao.wallpaper.portal.entity.vo.AlbumPicStatisVO 最新分享次数
     */
    @Override
    public AlbumPicStatisVO increShareService(String picId) {
        Boolean hasKey = redisTemplate.hasKey(RedisKeys.ALBUM_PIC_STATIS.getKey() + picId);
        AlbumPicStatisVO vo=null;
        //如果没有key则说明未缓存过数据
        if(!hasKey){
            WallpaperAlbumPic wallpaperAlbumPic = albumPicMapper.selectById(picId);
            vo=new AlbumPicStatisVO();
            vo.setId(wallpaperAlbumPic.getId());
            vo.setHot(Long.parseLong(wallpaperAlbumPic.getHot()));
            vo.setShare(Long.parseLong(wallpaperAlbumPic.getShare()));

        }else{
            String s = redisTemplate.opsForValue().get(RedisKeys.ALBUM_PIC_STATIS.getKey() + picId);
            vo = JSONObject.parseObject(s, AlbumPicStatisVO.class);
        }
        vo.setShare(vo.getShare()+1);
        redisTemplate.opsForValue().set(RedisKeys.ALBUM_PIC_STATIS.getKey()+picId,JSONObject.toJSONString(vo));

        return vo;

    }

    /**
     * 获得照片分享总数
     *
     * @param picId : 图片ID
     * @Author QLZ
     * @Date 2021/2/26
     * @return: java.lang.String 分享总数
     */
    @Override
    public String getShareCountService(String picId) {
        String s = redisTemplate.opsForValue().get(RedisKeys.ALBUM_PIC_STATIS.getKey() + picId);
        AlbumPicStatisVO albumPicStatisVO = JSONObject.parseObject(s, AlbumPicStatisVO.class);
        Long share = albumPicStatisVO.getShare();

        return share.toString();
    }


}
